<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  data() {
    return {
      appWindow: window,
      form: {
        discountType: 1, //单选
        minSalePrice: "", //销售价
        minOriginalPrice: "", //成本价
        discountValue: "", //折扣
      },

      apiUrl: {
        detail: "mall-site-portal/member-rights/getRuleProductItem",
        update: "mall-site-portal/member-rights/updateRuleProduct",
      },

      hasPrice: false, //控制 销售价和成本价显示隐藏

      loading: true,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      this.mixinEdit_editVisible = true;
      this.loading = true;
      if (this.type === "single") {
        await this.getItem(this.idArr[0]); //因为是单个 所以直接选第一项   从ruleGoodsListDialog 传来
        this.hasPrice = true;
      } else if (this.type === "batch") {
        this.hasPrice = false;
      }

      this.loading = false;
    },

    // 获取详情
    async getItem(id) {
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.detail,
          data: {
            rightRuleId: this.rightRuleId, //从ruleGoodsListDialog 传来  权益规则id
            id: id,
          },
        },
        window
      );

      if (res._ok) {
        for (const key in this.form) {
          if (!this.$vFN.Utils.isEmpty(res.data[key])) {
            this.form[key] = res.data[key];
          }
        }

        console.log(this.form);
      }
    },

    // 输入框校验
    validator(rule, value, callback) {
      if (value > 10) {
        callback(new Error("最大折扣为10"));
      } else if (value === 0) {
        callback(new Error("最小折扣不能为0"));
      } else {
        this.$vFN.Tools.validateMoney(rule, value, callback);
      }
    },

    validate() {
      let that = this;
      return new Promise((resolve, reject) => {
        that.$refs["editForm"].validate((valid) => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },

    // 提交
    async submit() {
      if (!(await this.validate())) {
        return;
      }

      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.update,
          data: {
            rightsRuleProductIdLst: this.idArr, //从ruleGoodsListDialog 传来  记录的id数组
            discountType: this.form.discountType,
            discountValue: this.form.discountValue,
          },
        },
        window
      );

      if (res._ok) {
        this.$vFN.Tools.messageSuccess(res.message);
        this.mixinEdit_editVisible = false;
        this.onSuccess();
      }
    },

    // 成功回调
    onSuccess() {},
  },
};
</script>
